//
//  MeViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "MeViewController.h"
#import "MeLoginAndRegistTableViewCell.h"
#import "AppDelegate.h"
#import "User.h"
#import "RequestManager.h"
#import "UserInfoTableViewCell.h"
#import "ThreeButtonTableViewCell.h"
#import "MeMianViewModel.h"

@interface MeViewController ()<UITableViewDelegate, UITableViewDataSource,UIScrollViewDelegate,RegistAndLoginDelegate, RequestDelegate,WBHttpRequestDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *MainTableView;
@property (nonatomic, strong) UIView *iconView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) User *user;

@property (nonatomic, strong) UITableView *loginTableView;

@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *listArrayOne;
@property (nonatomic, strong) NSMutableArray *listArrayTwo;
@property (nonatomic, strong) NSMutableArray *listArrayThree;
@property (nonatomic, strong) NSMutableArray *listArrayFour;
@property (nonatomic, strong) NSMutableArray *listArrayFive;


@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listArray = [NSMutableArray array];
    self.listArrayTwo = [NSMutableArray array];
    self.listArrayThree = [NSMutableArray array];
    self.listArrayFour = [NSMutableArray array];
    self.listArrayFive = [NSMutableArray array];
    
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我";
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"设置" style: UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    if (!accessToken) {
        [self setUnloginStateView];
    }else {
        [self requestForUserProfile];
    
    }
    
    [RequestManager requestWithUrl:kMeMainViewUrl requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *cardsArray = dic[@"cards"];
        for (int i = 0; i < 5; i++) {
            NSDictionary *cardsDic = cardsArray[i];
            NSArray *card_groupArray = cardsDic[@"card_group"];
            for (NSDictionary *dic in card_groupArray) {
                MeMianViewModel *model = [[MeMianViewModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.listArray addObject:model];
            }
        }
        [self.listArrayTwo addObject:self.listArray[2]];
        [self.listArrayTwo addObject:self.listArray[3]];
        [self.listArrayThree addObject:self.listArray[4]];
        [self.listArrayThree addObject:self.listArray[5]];
        [self.listArrayFour addObject:self.listArray[6]];
        [self.listArrayFive addObject:self.listArray[7]];
        
    } error:nil];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([tableView isEqual:_tableView]) {
        return 1;
    }else {
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:_tableView]) {
        return 1;
    }else {
        return 2;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_tableView]) {
        MeLoginAndRegistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeLoginAndRegistTableViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        
       
            if (indexPath.row == 0) {
                UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoTableViewCell" forIndexPath:indexPath];
                cell.user = _user;
                return cell;
            }else {
                ThreeButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeButtonTableViewCell" forIndexPath:indexPath];
                cell.user = _user;
                return cell;
            }
        
       
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([tableView isEqual:_tableView]) {
        return 40;
    }else {
        return 20;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:_tableView]) {
        return kHeight - 240 - 64;
    }else {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                return 75;
            }else{
                return 55;
            }
        }else{
            return 100;
        }
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if ([tableView isEqual:_tableView]) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
        UILabel *bigLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 34, 30)];
        bigLabel.text = @"关注";
        [view addSubview:bigLabel];
        UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, 200, 30)];
        smallLabel.text = @"快看看大家都在关注谁";
        smallLabel.font = [UIFont systemFontOfSize:13];
        smallLabel.textColor = [UIColor grayColor];
        view.backgroundColor = [UIColor whiteColor];
        [view addSubview:smallLabel];
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 25, 5, 17, 30)
                               ];
        rightLabel.text= @">";
        [view addSubview:rightLabel];
        
        return view;
    }else {
        return nil;
    }
   
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%f",offsetY);
    
}

- (void)rightButtonAction:(UIBarButtonItem *)button {
    NSLog(@"设置");
}
- (void)registAction:(UIButton *)button {
    [self regist];
}


- (void)regist {
    [WeiboSDK messageRegister:@"验证码登陆"];
}
- (void)login {
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.myDelegate = self;
    
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURL;
    request.scope = @"all";
    [WeiboSDK sendRequest:request];

}

- (void)requestReceiveWithState:(NSInteger)state {
    NSLog(@"%ld",state);
    if (state == 0) {
       [self requestForUserProfile];
    }

}
- (void)requestForUserProfile
{
    
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    
    [WBHttpRequest requestWithURL:@"https://api.weibo.com/2/users/show.json" httpMethod:@"GET" params:@{@"access_token":accessToken,@"uid":userID} delegate:self withTag:@"本人信息"];
   
}
- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data {
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    self.user = [[User alloc] init];
    [self.user setValuesForKeysWithDictionary:dic];
    
    
    [self.iconView removeFromSuperview];
    [self.headImageView removeFromSuperview];
    [self.tableView removeFromSuperview];
    [self setLoginStateView];
    
}


- (void)setLoginStateView {
    self.loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    self.loginTableView.delegate = self;
    self.loginTableView.dataSource = self;
    [self.view addSubview:_loginTableView];
    [self.loginTableView registerClass:[UserInfoTableViewCell class] forCellReuseIdentifier:@"UserInfoTableViewCell"];
    [self.loginTableView registerClass:[ThreeButtonTableViewCell class] forCellReuseIdentifier:@"ThreeButtonTableViewCell"];
    [self.view addSubview:_loginTableView];
    
}
- (void)setUnloginStateView {
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -10, kWidth, 300)];
    _headImageView.image = [UIImage imageNamed:@"me_bg.jpg"];
    
    [self.view addSubview:_headImageView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kWidth, kHeight - 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.rowHeight = kHeight - 240 - 64;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"MeLoginAndRegistTableViewCell" bundle:nil] forCellReuseIdentifier:@"MeLoginAndRegistTableViewCell"];
    
    _iconView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    _iconView.backgroundColor = [UIColor clearColor];
    UIButton *iconButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    iconButton.frame = CGRectMake(0, 0, 60, 60);
    [iconButton setBackgroundImage:[UIImage imageNamed:@"head.png"] forState:UIControlStateNormal];
    iconButton.center = _iconView.center;
    [_iconView addSubview:iconButton];
    [iconButton addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    _tableView.tableHeaderView = _iconView;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
